//
//  MainTabbar.swift
//  Sinaweibo
//
//  Created by 李伟 on 16/2/17.
//  Copyright © 2016年 一苇渡江. All rights reserved.
//  自定义Tabbar

import UIKit

class MainTabbar: UITabBar {

    // 布局子视图方法
    override func layoutSubviews() {
        
        super.layoutSubviews() // 不调用父类的layoutSubviews方法 会造成tabBar透明
        
        // 确定TabbarButton的大小
        let w = self.bounds.width / 5
        let h = self.bounds.height
        
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        // 定义一个能自增的索引
        var index: CGFloat = 0
        
        // 遍历子视图 获取UITabbarButton
        for subView in subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                // 修改frame
                subView.frame = CGRectOffset(rect, w * index, 0)
                
                if index == 1 { // 多递增一次, 为按钮留位置
                    index++
                }
                
                index++
            }
            
        }
        
        plusBtn.frame = CGRectOffset(rect, w * 2, 0)
    }
    
    lazy var plusBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        self.addSubview(btn)
        
        return btn
        
    }()
    
}
